<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>H5之微信公众号分享 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="公众号分享,">
  

  <meta name="description" content="使用微信的分享功能，需要使用微信JS-SDK来完成。且只能点击微信右上角的...调起分享面板，不能直接由页面行为唤起！本教程使用js-sdk最新版  微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信JS-SDK，网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力，同时可以直接使用微信分享、扫一扫、卡券、支付等微信特有的能力 一、JSSD">
<meta name="keywords" content="公众号分享">
<meta property="og:type" content="article">
<meta property="og:title" content="H5之微信公众号分享">
<meta property="og:url" content="http://blog.poetries.top/2020/05/24/wx-share/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="使用微信的分享功能，需要使用微信JS-SDK来完成。且只能点击微信右上角的...调起分享面板，不能直接由页面行为唤起！本教程使用js-sdk最新版  微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信JS-SDK，网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力，同时可以直接使用微信分享、扫一扫、卡券、支付等微信特有的能力 一、JSSD">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.942Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="H5之微信公众号分享">
<meta name="twitter:description" content="使用微信的分享功能，需要使用微信JS-SDK来完成。且只能点击微信右上角的...调起分享面板，不能直接由页面行为唤起！本教程使用js-sdk最新版  微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信JS-SDK，网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力，同时可以直接使用微信分享、扫一扫、卡券、支付等微信特有的能力 一、JSSD">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、JSSDK使用步骤"><span class="toc-text">一、JSSDK使用步骤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、微信分享"><span class="toc-text">二、微信分享</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、分享接口"><span class="toc-text">三、分享接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、分享开发调试时注意事项"><span class="toc-text">四、分享开发调试时注意事项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、实战"><span class="toc-text">五、实战</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-wx-share" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">H5之微信公众号分享</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2020.05.24</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 3.4k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 13分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <blockquote>
<p>使用微信的分享功能，需要使用微信<code>JS-SDK</code>来完成。且只能点击微信右上角的<code>...</code>调起分享面板，不能直接由页面行为唤起！本教程使用js-sdk最新版</p>
</blockquote>
<p><a href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html" target="_blank" rel="noopener">微信JS-SDK</a>是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信<code>JS-SDK</code>，网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力，同时可以直接使用微信分享、扫一扫、卡券、支付等微信特有的能力</p>
<h2 id="一、JSSDK使用步骤"><a href="#一、JSSDK使用步骤" class="headerlink" title="一、JSSDK使用步骤"></a>一、JSSDK使用步骤</h2><p><strong>1. 步骤一：绑定域名</strong></p>
<blockquote>
<p>登录微信公众平台 –&gt; 公众号设置 –&gt; 功能设置 –&gt; 填写“JS接口安全域名”</p>
</blockquote>
<p><strong>2. 步骤二：引入JS文件</strong></p>
<ul>
<li>在需要调用JS接口的页面引入如下JS文件，（支持https）：<a href="http://res.wx.qq.com/open/js/jweixin-1.6.0.js" target="_blank" rel="noopener">http://res.wx.qq.com/open/js/jweixin-1.6.0.js</a></li>
<li>如需进一步提升服务稳定性，当上述资源不可访问时，可改访问：<a href="http://res2.wx.qq.com/open/js/jweixin-1.6.0.js" target="_blank" rel="noopener">http://res2.wx.qq.com/open/js/jweixin-1.6.0.js</a> （支持https）。</li>
<li>备注：支持使用 AMD/CMD 标准模块加载方法加载</li>
</ul>
<p><strong>3. 步骤三：通过config接口注入权限验证配置</strong></p>
<blockquote>
<p>所有需要使用JS-SDK的页面必须先注入配置信息，否则将无法调用</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">wx.config(&#123;</span><br><span class="line">  debug: <span class="literal">true</span>, <span class="comment">// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。</span></span><br><span class="line">  appId: <span class="string">''</span>, <span class="comment">// 必填，公众号的唯一标识</span></span><br><span class="line">  timestamp: , <span class="comment">// 必填，生成签名的时间戳</span></span><br><span class="line">  nonceStr: <span class="string">''</span>, <span class="comment">// 必填，生成签名的随机串</span></span><br><span class="line">  signature: <span class="string">''</span>,<span class="comment">// 必填，签名</span></span><br><span class="line">  jsApiList: [] <span class="comment">// 必填，需要使用的JS接口列表</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>config</code>是一个客户端的异步操作</li>
<li>在<code>JS-SDK</code>后调用，也应该尽可能早的调用</li>
<li>同一个<code>url</code>仅需调用一次</li>
<li>对于变化<code>url</code>的<code>SPA</code>的<code>web app</code>可在每次<code>url</code>变化时进行调用</li>
<li>低于<code>Android6.2</code>版本的微信客户端，不支持<code>pushState</code>的H5新特性，使用pushState来实现web app的页面会导致签名失败</li>
</ul>
<p><strong>4. 步骤四：通过ready接口处理成功验证</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">wx.ready(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">// config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>由于config是一个异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中</p>
</blockquote>
<p><strong>注</strong>：无论<code>config</code>成功或失败，<code>ready</code>中的内容都会被执行！</p>
<p><strong>5. 步骤五：通过error接口处理失败验证</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">wx.error(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">  <span class="comment">// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p><strong>6. 通用参数：</strong></p>
<blockquote>
<p>所有接口通过<code>wx</code>对象(也可使用<code>jWeixin</code>对象)来调用，参数是一个对象，除了每个接口本身需要传的参数之外，还有以下通用函数参数：</p>
</blockquote>
<ul>
<li><code>success</code> 接口调用成功时执行的回调函数。</li>
<li><code>fail</code> 接口调用失败时执行的回调函数。</li>
<li><code>complete</code> 接口调用完成时执行的回调函数，无论成功或失败都会执行。</li>
<li><code>cancel</code> 用户点击取消时的回调函数，仅部分有用户取消操作的api才会用到。</li>
<li><code>trigger</code> 监听Menu中的按钮点击时触发的方法，该方法仅支持Menu中的相关接口。</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 回调参数：</span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    xxx: xxx,</span><br><span class="line">    errMsg: <span class="string">''</span> <span class="comment">// 接口调用成功/失败信息</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="二、微信分享"><a href="#二、微信分享" class="headerlink" title="二、微信分享"></a>二、微信分享</h2><blockquote>
<p>用户调用微信的分享功能，可以自定义分享的title和描述，以及小图标和链接。可以分享到群、好友、朋友圈、QQ、QQ空间等</p>
</blockquote>
<p><strong>分享设计规范</strong></p>
<ul>
<li>分享标题：14字以内，建议使用朋友般亲切的口吻</li>
<li>分享图标：尺寸<code>120*120</code>，大小不超过<code>10K</code>，不支持<code>GIF</code>格式。必须采用<code>https</code>协议。</li>
<li>分享描述：<code>20</code>字以内，对标题的简要解读。</li>
<li>分享链接：外链页面所在服务器至少能支持每秒<code>1500</code>次的访问压力，且每次访问的响应时间<code>200ms</code>以内。必须采用<code>https</code>协议。</li>
<li>分享行为：<strong>页面上无分享按钮</strong>，页面上无诱导分享行为，包含但不限于分享后才能看到特定的信息，分享后才能进行下一步流程，分享后可以获得奖励等</li>
<li>分享文案：分享时“文案”和“图片”可以正常显示，分享后链接可以访问。</li>
<li>分享标题和描述不能出现敏感词汇，否则会导致部分不可预知的问题。比如分享者可以看到分享图标，被分享者看不到图标等。</li>
</ul>
<blockquote>
<p>敏感词举例：红包、现金、到账等</p>
</blockquote>
<p><strong>注：</strong> 分享的图标链接和分享链接尽量保持为同一域名下的资源。否则可能会出现分享不成功或分享图标不显示的情况。</p>
<blockquote>
<p>由于不能由页面直接唤起微信的分享面板，所以就需要一个弹窗浮层来引导用户用户去点击<code>...</code>按钮唤起分享面板。注意这个弹窗浮层不能出现诱导分享的内容。</p>
</blockquote>
<p><strong>分享或广告文案禁止内容：</strong></p>
<ul>
<li>特殊字符：不允许使用特殊字符与符号 ，例如：<code>“：）” “-。-”</code>等； 不允许使用 <code>emoji</code> 表情</li>
<li>诱导或引导操作： 不允许出现诱导或引导用户操作的描述。包含但不限于以下文案：“请点击查看详情”、 “赶快戳开看一看”、“点一下下面你就知道是什么”、“点击下方了解公众号”</li>
<li>微信产品功能词汇：未经微信官方授权，禁止使用以下产品功能词汇及其谐音词汇。包含但不限于以下内容：“朋友圈” 、“点赞” 、“评论” 、“公众号”、 “微信”、 “红包”</li>
<li><code>URL</code>：不允许直接放URL链接内容</li>
<li>电话号码：不允许出现电话号码</li>
<li>破折号：不允许出现破折号，破折号在移动端显示容易产生歧义</li>
<li>空行&amp;空格：不允许使用空行或空格</li>
<li>不规范折行：不允许出现单个词语或文字折行</li>
<li>股票代码：不允许出现公司股票代码</li>
<li>非简体中文文字&amp;方言&amp;小语种：不允许使用非简体中文文字（单字、词语、成语）。暂不支持使用方言和小语种作为文案。</li>
<li>产品销量数据：不允许使用任何维度的产品销量数据。</li>
</ul>
<h2 id="三、分享接口"><a href="#三、分享接口" class="headerlink" title="三、分享接口"></a>三、分享接口</h2><p><strong>3.1 自定义“分享给朋友”及“分享到QQ”按钮的分享内容（1.4.0）</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">wx.ready(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;   <span class="comment">//需在用户可能点击分享按钮前就先调用</span></span><br><span class="line">  wx.updateAppMessageShareData(&#123; </span><br><span class="line">    title: <span class="string">''</span>, <span class="comment">// 分享标题</span></span><br><span class="line">    desc: <span class="string">''</span>, <span class="comment">// 分享描述</span></span><br><span class="line">    link: <span class="string">''</span>, <span class="comment">// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致</span></span><br><span class="line">    imgUrl: <span class="string">''</span>, <span class="comment">// 分享图标</span></span><br><span class="line">    success: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 设置成功</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p><strong>3.2 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容（1.4.0）</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">wx.ready(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;      <span class="comment">//需在用户可能点击分享按钮前就先调用</span></span><br><span class="line">  wx.updateTimelineShareData(&#123; </span><br><span class="line">    title: <span class="string">''</span>, <span class="comment">// 分享标题</span></span><br><span class="line">    link: <span class="string">''</span>, <span class="comment">// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致</span></span><br><span class="line">    imgUrl: <span class="string">''</span>, <span class="comment">// 分享图标</span></span><br><span class="line">    success: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 设置成功</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="四、分享开发调试时注意事项"><a href="#四、分享开发调试时注意事项" class="headerlink" title="四、分享开发调试时注意事项"></a>四、分享开发调试时注意事项</h2><ul>
<li>分享出去的外链的域名必须和公众号后台配置的JS安全域名一致，否则会导致分享的失败</li>
<li>享出去的外链，会自动给加上微信标识，导致二次分享失败</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">如打开页面：</span><br><span class="line"></span><br><span class="line">https://www.xxx.com/m/#/activity/invite/friends</span><br><span class="line">分享出去链接：</span><br></pre></td></tr></table></figure>
<blockquote>
<p><a href="https://www.xxx.com/m/?from=groupmessage&amp;isappinstalled=0#/activity/invite/friends" target="_blank" rel="noopener">https://www.xxx.com/m/?from=groupmessage&amp;isappinstalled=0#/activity/invite/friends</a> 微信自动在分享后边加上了<code>query</code>字符串：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">from=groupmessage   分享到群</span><br><span class="line">from=timeline  分享到朋友圈</span><br><span class="line">from=singlemessage  分享到好友</span><br><span class="line">isappinstalled=0    0或1，表示是否安装了app</span><br></pre></td></tr></table></figure>
<p><strong>注：安卓手机分享到朋友圈的链接</strong>，只会带<code>from=timeline</code></p>
<blockquote>
<p>由于微信的签名生成时，需要传一个<code>url</code>参数，而这个<code>url</code>则是通过</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">location.href.split(<span class="string">'#'</span>)[<span class="number">0</span>]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>获取的<code>url</code>，取的是<code>url</code>的<code>#</code>前边的部分来生成签名，第一次分享成功，生成签名的<code>url</code>不带<code>query</code>字段。通过一次分享出去的链接，带上了<code>query</code>后，生成的签名就无效了，导致二次分享失败。</p>
</blockquote>
<p><strong>解决办法：</strong></p>
<ol>
<li>替换路径</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> href = <span class="built_in">window</span>.location.href;</span><br><span class="line"><span class="keyword">if</span>(href.indexOf(<span class="string">'groupmessage'</span>) &gt; <span class="number">-1</span> || href.indexOf(<span class="string">'singlemessage'</span>) &gt; <span class="number">-1</span> || href.indexOf(<span class="string">'timeline'</span>) &gt; <span class="number">-1</span>)&#123;</span><br><span class="line">    href = href.replace(<span class="regexp">/\?from=(groupmessage|singlemessage|timeline)(\S*)#/</span>, <span class="string">'#'</span>);</span><br><span class="line">    <span class="built_in">window</span>.location.href = href;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>不过这样，会导致页面请求两次，细心的用户可能会感知到。或者用户网络不稳定时，可能他会感觉到两次页面的刷新</p>
</blockquote>
<ol start="2">
<li>生成签名的时候，动态的获取url，传给生成签名的接口</li>
</ol>
<blockquote>
<p>每次打开页面时，都获取到url的#前边部分传给签名生成接口，保证每次的签名都是有效的</p>
</blockquote>
<h2 id="五、实战"><a href="#五、实战" class="headerlink" title="五、实战"></a>五、实战</h2><blockquote>
<p>hash路由形式分享链接到朋友圈，需要特殊处理url</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 请求后台接口 获取微信配置信息</span></span><br><span class="line">*getWxSignature(&#123; payload, callback &#125;, &#123; call, put, select &#125;) &#123;</span><br><span class="line">  <span class="keyword">const</span> res = <span class="keyword">yield</span> call(rGetWxSign, payload);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(res.code !==<span class="number">0</span>) &#123;<span class="keyword">return</span>;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 通过config接口注入权限验证配置</span></span><br><span class="line">  wx.config(&#123;</span><br><span class="line">    debug: <span class="literal">false</span>, <span class="comment">// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。</span></span><br><span class="line">    appId: res.data.appId, <span class="comment">// 必填，公众号的唯一标识</span></span><br><span class="line">    timestamp: res.data.timestamp, <span class="comment">// 必填，生成签名的时间戳</span></span><br><span class="line">    nonceStr: res.data.nonceStr, <span class="comment">// 必填，生成签名的随机串</span></span><br><span class="line">    signature: res.data.signature, <span class="comment">// 必填，签名，见附录1</span></span><br><span class="line">    jsApiList: [</span><br><span class="line">      <span class="string">'updateTimelineShareData'</span>, </span><br><span class="line">      <span class="string">'updateAppMessageShareData'</span></span><br><span class="line">    ] <span class="comment">// 必填，需要使用的JS接口列表，所有JS接口列表见附录2</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 获取微信签名</span></span><br><span class="line">getWxSignature() &#123;</span><br><span class="line">  <span class="keyword">let</span> url = location.href</span><br><span class="line">  <span class="comment">// 如果页面url是hash路由形式 需要处理一下参数</span></span><br><span class="line">  <span class="keyword">let</span> i = url.indexOf(<span class="string">'#'</span>)</span><br><span class="line">  <span class="keyword">if</span> (i !== <span class="number">-1</span>) &#123;</span><br><span class="line">    url = url.substring(<span class="number">0</span>, i)</span><br><span class="line">  &#125;</span><br><span class="line">  dispatch(&#123; </span><br><span class="line">    type: <span class="string">`<span class="subst">$&#123;namespace&#125;</span>/getWxSignature`</span>, </span><br><span class="line">    payload: &#123;url&#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 页面加载完成就执行初始化监听</span></span><br><span class="line">handleWxShare = <span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> &#123;<span class="attr">pagedata</span>: &#123;mainData=&#123;&#125;,projectInfo=&#123;&#125;&#125;&#125; = <span class="keyword">this</span>.props;</span><br><span class="line">    <span class="keyword">const</span> &#123;pagequery&#125; = <span class="keyword">this</span>.state</span><br><span class="line"></span><br><span class="line">    wx.ready(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">'wx.ready:'</span>,projectInfo)</span><br><span class="line">      <span class="comment">// config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。</span></span><br><span class="line">      <span class="keyword">let</span> linkUrl = location.href</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 监听右上角分享到朋友圈事件</span></span><br><span class="line">      <span class="comment">// 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容（1.4.0）</span></span><br><span class="line">      wx.updateTimelineShareData(&#123;</span><br><span class="line">        title: mainData.title, <span class="comment">// 分享标题</span></span><br><span class="line">        desc: (projectInfo.project || &#123;&#125;).projectName || <span class="string">''</span>, <span class="comment">// 分享描述</span></span><br><span class="line">        link: linkUrl, <span class="comment">// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致</span></span><br><span class="line">        imgUrl: mainData.sharePicUrl, <span class="comment">// 分享图标</span></span><br><span class="line">        success: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="comment">// 用户确认分享后执行的回调函数</span></span><br><span class="line">          <span class="built_in">console</span>.log(<span class="string">'分享成功'</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        cancel: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="comment">// 用户取消分享后执行的回调函数</span></span><br><span class="line">          <span class="built_in">console</span>.log(<span class="string">'取消分享'</span>)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">       <span class="comment">// 监听右上角发送给朋友事件</span></span><br><span class="line">       <span class="comment">// 自定义“分享给朋友”及“分享到QQ”按钮的分享内容</span></span><br><span class="line">       wx.updateAppMessageShareData(&#123;</span><br><span class="line">        title: mainData.title, <span class="comment">// 分享标题</span></span><br><span class="line">        desc: (projectInfo.project || &#123;&#125;).projectName || <span class="string">''</span>, <span class="comment">// 分享描述</span></span><br><span class="line">        link: linkUrl, <span class="comment">// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致</span></span><br><span class="line">        imgUrl: mainData.sharePicUrl, <span class="comment">// 分享图标</span></span><br><span class="line">        success: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="comment">// 用户确认分享后执行的回调函数</span></span><br><span class="line">          <span class="built_in">console</span>.log(<span class="string">'分享成功'</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        cancel: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="comment">// 用户取消分享后执行的回调函数</span></span><br><span class="line">          <span class="built_in">console</span>.log(<span class="string">'取消分享'</span>)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    wx.error(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">      <span class="comment">// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。</span></span><br><span class="line">      <span class="built_in">console</span>.log(res,<span class="string">'wx error'</span>)</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">componentDidMount() &#123;</span><br><span class="line">    <span class="comment">// 微信分享到朋友圈、发送朋友分享的链接带上微信加上的参数 导致分享不了 </span></span><br><span class="line">    <span class="keyword">let</span> href = <span class="built_in">window</span>.location.href</span><br><span class="line">    <span class="comment">// from=groupmessage   分享到群</span></span><br><span class="line">    <span class="comment">// from=timeline  分享到朋友圈</span></span><br><span class="line">    <span class="comment">// from=singlemessage  分享到好友</span></span><br><span class="line">    <span class="keyword">if</span>(~href.indexOf(<span class="string">'from=timeline'</span>) || ~href.indexOf(<span class="string">'from=singlemessage'</span>) || ~href.indexOf(<span class="string">'from=groupmessage'</span>)) &#123;</span><br><span class="line">      <span class="comment">// 带有hash路由的链接分享到朋友圈会被微信带上?from=timeline 导致二次分享的链接打不开 需要重新处理</span></span><br><span class="line">      <span class="comment">// 如果是history模式下的路由 不需要另外处理</span></span><br><span class="line">      <span class="comment">// https://zmjtest.kaisasso.com/wxapp/?from=timeline#/dsb/article?id=286&amp;pid=20&amp;uid=110</span></span><br><span class="line">      <span class="built_in">window</span>.location.href = <span class="string">`<span class="subst">$&#123;location.origin&#125;</span><span class="subst">$&#123;location.pathname&#125;</span><span class="subst">$&#123;location.hash&#125;</span>`</span> </span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2020/03/26/fe-debug/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2020/06/14/weapp-live-summary/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
